import numpy as np
import os, sys, json


class DefectAnno:
    def __init__(self, file_path):
        self.file_path = file_path
        self.Load()

    def ReFormatAnno(self):
        self.annotationsReformated = {}
        for anno in self.annotations:
            self.annotationsReformated[anno['image_id']] = anno

    def Load(self):
        dictAnno = json.load(open(self.file_path, "r"))

        self.images = dictAnno['images']  # {file_name:xxx, id: 1, height: xxx, width:xxx}
        self.annotations = dictAnno[
            'annotations']  # {area: 2000, iscrowd: 0 , image_id: 1, bbox:[x,y,w,h], 'category_id':2, 'id': 1 }
        # TODO: annotation exist another id , with should be standard.
        self.categories = dictAnno['categories']  # { supercategory: 'po shun', id: 1 , name: 'p shun'}
        self.ReFormatAnno()

        # " file name , categoryId , bbox
        self.all_pairs = []
        size = len(self.images)
        for i, image in enumerate(self.images):
            self.all_pairs.append([image['file_name'],
                                   self.annotationsReformated[image['id']]['category_id'],
                                   self.annotationsReformated[image['id']]['bbox']]
                                  )
            print(" appending image {} {} {} {}/{}".format(image['file_name'],
                                                           self.annotationsReformated[image['id']]['category_id'],
                                                           self.annotationsReformated[image['id']]['bbox'],
                                                           i, size))
            assert (self.annotationsReformated[image['id']]['image_id'] == image['id'])

        print(" load complete size {}".format())
        return


if __name__ == '__main__':
    annos = DefectAnno("./annotations.json")
